package com.mayi.zds.service.imp;

import com.alibaba.fastjson.JSON;
import com.mayi.zds.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ZdsWebsocketHandler
 * @Author zhong
 * @Date 2019/12/25 17:00:49
 * @Version 1.0
 */
@Service
public class ZdsWebsocketHandler extends TextWebSocketHandler {
    //在线用户列表
    private static final Map<String, WebSocketSession> businesses;

    @Autowired
    private ScanOrderStatusServiceImp scanOrderStatusService;

    static {
        businesses = new HashMap<String, WebSocketSession>();
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        String id = (String) session.getAttributes().get("id");
        businesses.put(id, session);

        session.sendMessage(new TextMessage("连接成功。"));
        System.out.println("成功建立连接");
    }

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) {
        Message message1 = JSON.parseObject(message.getPayload(), Message.class);
        if("yes".equals(message1.getContent())){
            scanOrderStatusService.cancleTask(message1.getOrderId());
        }
    }

    /**
     * 发送信息给指定用户
     * @param busiId 商户的ID
     * @param message 消息内容
     * @return
     */
    public boolean sendMessageToBusiness (String busiId, TextMessage message) {
        WebSocketSession session = businesses.get(busiId);
        if ( session == null) {
            System.out.println("商户"+busiId+"未上线。");
            return false;
        }

        System.out.println("sendMessage:" + session);
        if (!session.isOpen()) {
            System.out.println("和 "+ busiId +" 的会话已经关闭");
            return false;
        }
        try {
            session.sendMessage(message);
        } catch (IOException e) {
            System.out.println("给 "+ busiId +" 发送消息失败。");
            return false;
        }
        return true;
    }



    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
        if (session.isOpen()) {
            session.close();
        }
        System.out.println("连接出错");
        String id = (String) session.getAttributes().get("id");
        businesses.remove(id, session);
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("连接已关闭：" + status);
        String id = (String) session.getAttributes().get("id");
            businesses.remove(id, session);
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }

}
